﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LFM_Playlist_Builder
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            initializeSettings();
        }

        public void initializeSettings()
        {
            txtUser.Text = Properties.Settings.Default.username;
            nmFetch.Value = Properties.Settings.Default.fetchSize;
            nmRecFetch.Value = Properties.Settings.Default.recFetchSize;
            nmLovedAFetch.Value = Properties.Settings.Default.lovedAFetchSize;
            nmSim.Value = Properties.Settings.Default.similarity;
            chkSim.Checked = Properties.Settings.Default.songNames;
            chkSimA.Checked = Properties.Settings.Default.artistNames;
            chkSimA.Enabled = chkSim.Checked;
            chkShowCorrected.Checked = Properties.Settings.Default.showCorrections;
            chkShowNotFound.Checked = Properties.Settings.Default.showNotFound;
            chkShowLovedA.Checked = Properties.Settings.Default.showLovedA;
            chkPlaycount.Checked = Properties.Settings.Default.sortPlaycount;

            nmSim.Enabled = (chkSim.Checked || chkSimA.Checked);
            chooseLoad(Properties.Settings.Default.loadChoice);
            txtPlaylist.Text = Properties.Settings.Default.playlistFilePath;
            if (Properties.Settings.Default.libraryFilePath == null || Properties.Settings.Default.libraryFilePath == "")
            {
                //Give default path of library
                Properties.Settings.Default.libraryFilePath = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Winamp\Plugins\ml\main.idx");
            }
            txtMedia.Text = Properties.Settings.Default.libraryFilePath;

            switch (Properties.Settings.Default.loadChoice)
            {
                case Global.LOADLIBRARY:
                    rdMediaLibrary.Checked = true;
                    break;
                case Global.LOADPLAYLIST:
                    rdPlaylist.Checked = true;
                    break;
                case Global.LOADCHOOSE:
                    rdAsk.Checked = true;
                    break;
                default:
                    rdAsk.Checked = true;
                    break;
            }

            openFDPlaylist.Filter = "Playlist Files|*.m3u|All Files|*.*";
            openFDLibrary.Filter = "Nullsoft Media Library|main.dat|All Files|*.*";
        }

        private void chooseLoad(int choice)
        {
            switch (choice)
            {
                case Global.LOADLIBRARY:
                    txtPlaylist.Enabled = false;
                    btnPlaylistBrowse.Enabled = false;

                    txtMedia.Enabled = true;
                    btnMediaBrowse.Enabled = true;

                    break;
                case Global.LOADPLAYLIST:
                    txtMedia.Enabled = false;
                    btnMediaBrowse.Enabled = false;

                    txtPlaylist.Enabled = true;
                    btnPlaylistBrowse.Enabled = true;

                    break;
                case Global.LOADCHOOSE:
                    txtMedia.Enabled = false;
                    btnMediaBrowse.Enabled = false;
                    txtPlaylist.Enabled = false;
                    btnPlaylistBrowse.Enabled = false;

                    break;
                default:
                    txtMedia.Enabled = false;
                    btnMediaBrowse.Enabled = false;
                    txtPlaylist.Enabled = false;
                    btnPlaylistBrowse.Enabled = false;

                    break;
            }
        }

        private void txtUser_TextChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.username = txtUser.Text;
        }

        private void chkSim_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.songNames = chkSim.Checked;
            if (!(chkSimA.Enabled = chkSim.Checked))
            {
                chkSimA.Checked = false;
            }
            nmSim.Enabled = (chkSim.Checked || chkSimA.Checked);
        }

        private void chkSimA_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.artistNames = chkSimA.Checked;
            nmSim.Enabled = (chkSim.Checked || chkSimA.Checked);
        }

        private void nmFetch_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.fetchSize = (int)nmFetch.Value;
        }

        private void nmSim_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.similarity = nmSim.Value;
        }

        private void btnAuth_Click(object sender, EventArgs e)
        {
            LastFMXML lastInit = new LastFMXML();
            Properties.Settings.Default.sessionKey = lastInit.authenticate();
            Properties.Settings.Default.Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            this.Close();
        }

        private void btnMediaBrowse_Click(object sender, EventArgs e)
        {
            if (openFDLibrary.ShowDialog() == DialogResult.OK)
            {
                txtMedia.Text = openFDLibrary.FileName;
                Properties.Settings.Default.libraryFilePath = openFDLibrary.FileName;
            }
        }

        private void btnPlaylistBrowse_Click(object sender, EventArgs e)
        {
            if (openFDPlaylist.ShowDialog() == DialogResult.OK)
            {
                txtPlaylist.Text = openFDPlaylist.FileName;
                Properties.Settings.Default.playlistFilePath = openFDPlaylist.FileName;
            }
        }

        private void rdMediaLibrary_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.loadChoice = Global.LOADLIBRARY;
            chooseLoad(Global.LOADLIBRARY);
        }

        private void rdPlaylist_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.loadChoice = Global.LOADPLAYLIST;
            chooseLoad(Global.LOADPLAYLIST);
        }

        private void rdAsk_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.loadChoice = Global.LOADCHOOSE;
            chooseLoad(Global.LOADCHOOSE);
        }

        private void openFDPlaylist_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void txtMedia_TextChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.libraryFilePath = txtMedia.Text;
        }

        private void txtPlaylist_TextChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.playlistFilePath = txtPlaylist.Text;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            initializeSettings();
            this.Close();
        }

        private void nmRecFetch_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.recFetchSize = (int)nmRecFetch.Value;
        }

        private void nmLovedAFetch_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.lovedAFetchSize = (int)nmLovedAFetch.Value;
        }

        private void chkShowCorrected_CheckedChanged(object sender, EventArgs e)
        {
             Properties.Settings.Default.showCorrections = chkShowCorrected.Checked;
        }

        private void chkShowNotFound_CheckedChanged(object sender, EventArgs e)
        {
             Properties.Settings.Default.showNotFound = chkShowNotFound.Checked;
        }

        private void chkShowLovedA_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.showLovedA = chkShowLovedA.Checked;
        }

        private void chkPlaycount_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.sortPlaycount = chkPlaycount.Checked;
        }
    }
}
